package com.lxb.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxb.commonutils.R;
import com.lxb.eduservice.dto.TeacherQueryDto;
import com.lxb.eduservice.entity.EduTeacher;
import com.lxb.eduservice.service.EduTeacherService;
import com.lxb.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lixuebin
 * @since 2022-10-05
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    /**
     * 查询所有讲师表数据
     *
     * @return
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R FindAllTeacher() {
        //调用service的方法实现查询所有的操作
        List<EduTeacher> list = teacherService.list(null);
        try {
//            int a= 1/0;
        } catch (Exception e) {
            //执行自定义异常
            throw new GuliException(20001,"执行自定义异常处理....");
        }
        return R.ok().data("intems", list);
    }

    /**
     * 按照教师id删除信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/{id}")
    public R removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 分页查询讲师方法
     *
     * @param current  当前页
     * @param limit    每页记录数
     * @return
     */
    @ApiOperation(value = "分页查询讲师方法")
    @GetMapping("/{current}/{limit}")
    public R pageList(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit) {
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        //实现分页
        //调用方法时,底层把分页的全部数据封装到teacherPage对象中
        teacherService.page(teacherPage, null);

        //总记录数
        long total = teacherPage.getTotal();
        //分页数据集合
        List<EduTeacher> records = teacherPage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 讲师多条件查询 + 分页
     *
     * @param current      当前页
     * @param limit        每页记录数
     * @param teacherQuery 教师查询实体
     * @return
     */
    @ApiOperation(value = "讲师多条件查询 + 分页")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit,
            @ApiParam(name = "teacherQuery", value = "教师查询对象", required = false)
            @RequestBody(required = false) TeacherQueryDto teacherQuery) {
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current, limit);

        //构建多条件查询
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        String level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //查询条件
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.lt("gmt_modified", end);
        }
        queryWrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        teacherService.page(teacherPage, queryWrapper);

        //总记录数
        long total = teacherPage.getTotal();
        //分页数据集合
        List<EduTeacher> records = teacherPage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 添加讲师接口
     *
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "添加讲师接口")
    @PostMapping("/addTeacher")
    public R addTeacher(@ApiParam(name = "eduTeacher",value = "讲师实体",required = true)@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        return save ? R.ok() : R.error();
    }

    /**
     * 根据讲师id 查询
     * @param id
     * @return
     */
    @GetMapping("/getTeacher/{id}")
    @ApiOperation(value = "根据讲师id查询")
    public R getTeacherById(@ApiParam(value = "讲师id", required = true) @PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    /**
     * 按id修改讲师信息
     * @param eduTeacher
     * @return
     */
    @ApiOperation("按id修改讲师信息")
    @PutMapping("/updateTeacher")
    public R updateTeacher(@ApiParam(value = "讲师实体",required = true) @RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        return b?R.ok():R.error();
    }
}

